// Hand.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

using namespace std;

xn::Context g_Context;
XnVSessionManager *g_pSessionManager = NULL;
XnVSwipeDetector  *g_pSwipe          = NULL;

int frame = 0;

void XN_CALLBACK_TYPE SessionStart(const XnPoint3D& ptFocus, void *pUserCxt)
{
	cout << "SessionStart" << endl;
}

void XN_CALLBACK_TYPE SessionEnd(void *pUserCxt)
{
	cout << "SessionEnd" << endl;
}

void XN_CALLBACK_TYPE SwipeLeft(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt)
{
	cout << "Swipe Left, " << "Velocity = " << fVelocity << endl;
}

void XN_CALLBACK_TYPE SwipeRight(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt)
{
	cout << "Swipe Right, " << "Velocity = " << fVelocity << endl;
}

void XN_CALLBACK_TYPE SwipeUp(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt)
{
	cout << "Swipe Up, " << "Velocity = " << fVelocity << endl;
}

void XN_CALLBACK_TYPE SwipeDown(XnFloat fVelocity, XnFloat fAngle, void* pUserCxt)
{
	cout << "Swipe Down, " << "Velocity = " << fVelocity << endl;
}

void XN_CALLBACK_TYPE SwipePointUpdate(const XnVHandPointContext* pContext, void* cxt)
{
	map<int, float> feature;

	feature[frame % 5] = pContext->ptPosition.X;
	feature[frame % 5 + 1] = pContext->ptPosition.Y;

	if (frame != 0 && frame % 5 == 0)
	{
		if (feature[0] < feature[2] && feature[2] < feature[4] && feature[4] < feature[6] && feature[6] < feature[8])
		{
			cout << "Right" << endl;
		}
	}
	//cout << "X = " << pContext->ptPosition.X << ", Y = " << pContext->ptPosition.Y <<endl;
}

#define SAMPLE_XML_FILE "C:/Program Files/Prime Sense/NITE/Data/Sample-Tracking.xml"
 
int main(int argc, char** argv)
{
	// Configure
	XnStatus rc = g_Context.InitFromXmlFile(SAMPLE_XML_FILE);
	if (rc != XN_STATUS_OK)
	{
		printf("Couldn't initialize from file: %s\n", xnGetStatusString(rc));
		return 1;
	}

	g_pSessionManager = new XnVSessionManager();
	rc = g_pSessionManager->Initialize(&g_Context, "Wave,Click", "RaiseHand");
	if (rc != XN_STATUS_OK)
	{
		printf("Couldn't initialize the Session Manager: %s\n", xnGetStatusString(rc));
		return 1;
	}
	g_pSessionManager->RegisterSession(NULL, &SessionStart, &SessionEnd);

	g_pSwipe = new XnVSwipeDetector;
	g_pSwipe->RegisterSwipeLeft(NULL, &SwipeLeft);
	g_pSwipe->RegisterSwipeRight(NULL, &SwipeRight);
	g_pSwipe->RegisterSwipeUp(NULL, &SwipeUp);
	g_pSwipe->RegisterSwipeDown(NULL, &SwipeDown);
	g_pSwipe->RegisterPointUpdate(NULL, &SwipePointUpdate);

	g_pSessionManager->AddListener(g_pSwipe);
	g_Context.StartGeneratingAll();

	while (true)
	{
		//cout <<	"Frame " << frame << endl;
		g_Context.WaitAndUpdateAll();
		// Process the data
		g_pSessionManager->Update(&g_Context);
		frame = frame + 1;
	}
}